package com.jwy.calculator_android;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.lang.reflect.Field;

public class MainActivity extends Activity {
    private Button number1, number2, number3, number4, number5, number6, number7, number8, number9, number0, number00;
    private Button clear, backspace, add, subtract, multiply, divide, per, dot, calculate;
    private TextView show_view;
    private static String result = "", processedData = "", exresult = "0";
    private static int temp;// 1,2,3,4 代表加减乘除

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.portrait_view);

        //判断屏幕状态，分别显示不同界面
        int width = getWindowManager().getDefaultDisplay().getWidth();
        int height = getWindowManager().getDefaultDisplay().getHeight();
        if (width < height) {
            setContentView(R.layout.portrait_view);
            set();
            Set_Portrait_View();
        } else {
            setContentView(R.layout.landscape_view);
            set();
            Set_Landscape_View();
        }
    }

    public static int getStatusBarHeight(Context context) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

    public void set() {
        //实例化各个对象
        show_view = (TextView) findViewById(R.id.show_view);
        number0 = (Button) findViewById(R.id.number0);
        number1 = (Button) findViewById(R.id.number1);
        number2 = (Button) findViewById(R.id.number2);
        number3 = (Button) findViewById(R.id.number3);
        number4 = (Button) findViewById(R.id.number4);
        number5 = (Button) findViewById(R.id.number5);
        number6 = (Button) findViewById(R.id.number6);
        number7 = (Button) findViewById(R.id.number7);
        number8 = (Button) findViewById(R.id.number8);
        number9 = (Button) findViewById(R.id.number9);
        clear = (Button) findViewById(R.id.clear);
        backspace = (Button) findViewById(R.id.backspace);
        add = (Button) findViewById(R.id.add);
        subtract = (Button) findViewById(R.id.subtract);
        multiply = (Button) findViewById(R.id.multiply);
        divide = (Button) findViewById(R.id.divide);
        dot = (Button) findViewById(R.id.dot);
        calculate = (Button) findViewById(R.id.calculate);
    }


    public void Set_Portrait_View() {
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;
        int height = metric.heightPixels;

        height -= getStatusBarHeight(this);

//        int width = getWindowManager().getDefaultDisplay().getWidth();
//        int height = getWindowManager().getDefaultDisplay().getHeight();

        show_view.setWidth(width);
        show_view.setHeight(height / 4);
        show_view.setTextSize(50);
        number0.setWidth(width / 2);
        number0.setHeight(height * 3 / 20);
        number0.setTextSize(40);
        number1.setWidth(width / 4);
        number1.setHeight(height * 3 / 20);
        number1.setTextSize(40);
        number2.setWidth(width / 4);
        number2.setHeight(height * 3 / 20);
        number2.setTextSize(40);
        number3.setWidth(width / 4);
        number3.setHeight(height * 3 / 20);
        number3.setTextSize(40);
        number4.setWidth(width / 4);
        number4.setHeight(height * 3 / 20);
        number4.setTextSize(40);
        number5.setWidth(width / 4);
        number5.setHeight(height * 3 / 20);
        number5.setTextSize(40);
        number6.setWidth(width / 4);
        number6.setHeight(height * 3 / 20);
        number6.setTextSize(40);
        number7.setWidth(width / 4);
        number7.setHeight(height * 3 / 20);
        number7.setTextSize(40);
        number8.setWidth(width / 4);
        number8.setHeight(height * 3 / 20);
        number8.setTextSize(40);
        number9.setWidth(width / 4);
        number9.setHeight(height * 3 / 20);
        number9.setTextSize(40);
        clear.setWidth(width / 4);
        clear.setHeight(height * 3 / 20);
        clear.setTextSize(40);
        backspace.setWidth(width / 4);
        backspace.setHeight(height * 3 / 20);
        backspace.setTextSize(40);
        add.setWidth(width / 4);
        add.setHeight(height * 3 / 20);
        add.setTextSize(40);
        subtract.setWidth(width / 4);
        subtract.setHeight(height * 3 / 20);
        subtract.setTextSize(40);
        multiply.setWidth(width / 4);
        multiply.setHeight(height * 3 / 20);
        multiply.setTextSize(40);
        divide.setWidth(width / 4);
        divide.setHeight(height * 3 / 20);
        divide.setTextSize(40);
        dot.setWidth(width / 4);
        dot.setHeight(height * 3 / 20);
        dot.setTextSize(40);
        calculate.setWidth(width / 4);
        calculate.setHeight(height * 3 / 10);
        calculate.setTextSize(40);
    }

    public void Set_Landscape_View() {
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;
        int height = metric.heightPixels;
        height -= getStatusBarHeight(this);
//        int width = getWindowManager().getDefaultDisplay().getWidth();
//        int height = getWindowManager().getDefaultDisplay().getHeight();

        show_view.setWidth(width / 2);
        show_view.setHeight(height);
        show_view.setTextSize(60);
        number0.setWidth(width / 4);
        number0.setHeight(height / 5);
        number0.setTextSize(40);
        number1.setWidth(width / 8);
        number1.setHeight(height / 5);
        number1.setTextSize(40);
        number2.setWidth(width / 8);
        number2.setHeight(height / 5);
        number2.setTextSize(40);
        number3.setWidth(width / 8);
        number3.setHeight(height / 5);
        number3.setTextSize(40);
        number4.setWidth(width / 8);
        number4.setHeight(height / 5);
        number4.setTextSize(40);
        number5.setWidth(width / 8);
        number5.setHeight(height / 5);
        number5.setTextSize(40);
        number6.setWidth(width / 8);
        number6.setHeight(height / 5);
        number6.setTextSize(40);
        number7.setWidth(width / 8);
        number7.setHeight(height / 5);
        number7.setTextSize(40);
        number8.setWidth(width / 8);
        number8.setHeight(height / 5);
        number8.setTextSize(40);
        number9.setWidth(width / 8);
        number9.setHeight(height / 5);
        number9.setTextSize(40);
        clear.setWidth(width / 8);
        clear.setHeight(height / 5);
        clear.setTextSize(40);
        backspace.setWidth(width / 8);
        backspace.setHeight(height / 5);
        backspace.setTextSize(40);
        add.setWidth(width / 8);
        add.setHeight(height / 5);
        add.setTextSize(40);
        subtract.setWidth(width / 8);
        subtract.setHeight(height / 5);
        subtract.setTextSize(40);
        multiply.setWidth(width / 8);
        multiply.setHeight(height / 5);
        multiply.setTextSize(40);
        divide.setWidth(width / 8);
        divide.setHeight(height / 5);
        divide.setTextSize(40);
        dot.setWidth(width / 8);
        dot.setHeight(height / 5);
        dot.setTextSize(40);
        calculate.setWidth(width / 8);
        calculate.setHeight(height * 2 / 5);
        calculate.setTextSize(40);
    }

    public static String calculte(double processeddata, int temp) {
        double temporaryResult = Double.parseDouble(result);
        switch (temp) {
            case 1:
                temporaryResult += processeddata;
                break;
            case 2:
                temporaryResult -= processeddata;
                break;
            case 3:
                temporaryResult *= processeddata;
                break;
            case 4:
                if (processeddata != 0) {
                    temporaryResult /= processeddata;
                } else {
                    return "不能被0除！";
                }
                break;
            case 5:
                temporaryResult %= processeddata;
                break;
        }
        temp = 0;
        return Double.toString(temporaryResult);
    }

    public void calculteEvent() {
        if (result == "") {
            if (processedData != "") {
                result = processedData;
                processedData = "";
            } else {
                result = exresult;
            }
        } else if (processedData != "") {
            result = calculte(Double.parseDouble(processedData), temp);
            processedData = "";
            show_view.setText(result);
        }
    }

    public void numberEvent(String number) {
        if (processedData == "" || processedData.charAt(0) == '0'
                && processedData.length() == 1) {
            processedData = number;
        } else {
            processedData += number;
        }
        if (processedData != "")
            show_view.setText(processedData);
    }

    public void number1_event(View v) {
        numberEvent("1");
    }

    public void number2_event(View v) {
        numberEvent("2");
    }

    public void number3_event(View v) {
        numberEvent("3");
    }

    public void number4_event(View v) {
        numberEvent("4");
    }

    public void number5_event(View v) {
        numberEvent("5");
    }

    public void number6_event(View v) {
        numberEvent("6");
    }

    public void number7_event(View v) {
        numberEvent("7");
    }

    public void number8_event(View v) {
        numberEvent("8");
    }

    public void number9_event(View v) {
        numberEvent("9");

    }

    public void number0_event(View v) {
        numberEvent("0");
    }

    public void dot_event(View v) {
        int t = 0;
        if (processedData == "") {
            processedData = "0.";
        } else {
            for (int i = 0; i < processedData.length(); i++) {
                if (processedData.charAt(i) == '.') {
                    t = 1;
                    break;
                }
            }
            if (t == 0) {
                processedData += ".";
            }
        }
        if (processedData != "")
            show_view.setText(processedData);
    }


    public void add_event(View v) {
        calculteEvent();
        temp = 1;
    }

    public void subtract_event(View v) {
        calculteEvent();
        temp = 2;
    }

    public void multiply_event(View v) {
        calculteEvent();
        temp = 3;
    }

    public void divide_event(View v) {
        calculteEvent();
        temp = 4;
    }

    public void clear_event(View v) {
        result = "";
        exresult = "0";
        processedData = "";
        show_view.setText("0");
    }

    public void backspace_event(View v) {
        if (processedData.length() >= 2) {
            processedData = processedData.substring(0, processedData.length() - 1);
            show_view.setText(processedData);
        } else if (processedData.length() == 1) {
            processedData = "0";
            System.out.println(processedData.length());
            show_view.setText(processedData);
        }
    }

    public void calculate_event(View v) {
        if (result == "") {
            if (processedData == "") {
                show_view.setText(exresult);
            } else {
                result = processedData;
                exresult = result;
            }
        } else if (processedData != "") {
            result = calculte(Double.parseDouble(processedData), temp);
            if (result.charAt(0) >= '0' && result.charAt(0) <= '9') {
                exresult = result;
            }
            show_view.setText(result);
        } else {
            show_view.setText(result);
        }
        result = "";
        processedData = "";
    }
}